//
//  NewsNextVIew.m
//  kr_36
//
//  Created by lmt on 16/1/25.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NewsNextVIew.h"
#import "LMTAFNetwork.h"
#import "NewsDetailModel.h"
#import <UIImageView+WebCache.h>
#import "NewsDetailTableViewCell.h"

@interface NewsNextVIew ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *dataArr;/**< 作品信息 */

@property (nonatomic, retain) UIWebView *web;
@property (nonatomic, retain) UIView *head;
@property (nonatomic, retain) UILabel *titleLabel;/**< 标题 */

@property (nonatomic, retain) UIImageView *tou;/**< 作者头像 */
@property (nonatomic, retain) UILabel *zuo;/**< 作者名字 */
@property (nonatomic, retain) UILabel *summary;/**< 作者信息 */

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, assign) BOOL isH;/**< 记录隐藏 */

@end

@implementation NewsNextVIew

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 103)];
    [self.view addSubview:_web];
    
    // webView 头
    _web.scrollView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    
    self.head = [[UIView alloc] initWithFrame:CGRectMake(0, -200, self.view.frame.size.width, 200)];
    //_head.backgroundColor = [UIColor orangeColor];
    [_web.scrollView addSubview:_head];
    
    
    // 标题
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 100)];
    _titleLabel.numberOfLines = 0;
    _titleLabel.font = [UIFont systemFontOfSize:20];
    [self.head addSubview:_titleLabel];
    
    // 作者信息
    UIView *zuozhe = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    [self.head addSubview:zuozhe];
    
    self.tou = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 40, 40)];
    self.tou.layer.cornerRadius = 20;
    _tou.clipsToBounds = YES;
    [zuozhe addSubview:_tou];
    
    self.zuo = [[UILabel alloc] initWithFrame:CGRectMake(70, 20, 100, 40)];
    [zuozhe addSubview:self.zuo];
    
    self.summary = [[UILabel alloc] initWithFrame:CGRectMake(70, 60, 250, 40)];
    [zuozhe addSubview:_summary];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(self.view.frame.size.width - 60, 25, 50, 50);
    [button setImage:[UIImage imageNamed:@"xia@2x"] forState:(UIControlStateNormal)];
    [zuozhe addSubview:button];
    
    [button addTarget:self action:@selector(hidden:) forControlEvents:(UIControlEventTouchUpInside)];
    
    _isH = NO;
    
    // 创建按钮菜单 tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 300) style:(UITableViewStylePlain)];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.hidden = YES;
    
    [_tableView registerClass:[NewsDetailTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    // 数据解析 内容
    [self dataHander];
    
    // 数据解析 作者
    [self dataZuozhe];
    
}

- (void)hidden:(UIButton *)btn
{
    _isH = !_isH;
    
    if (_isH) {
        [btn setImage:[UIImage imageNamed:@"shang@2x"] forState:(UIControlStateNormal)];
        _tableView.hidden = NO;
        [_tableView reloadData];
    } else {
        [btn setImage:[UIImage imageNamed:@"xia@2x"] forState:(UIControlStateNormal)];
        _tableView.hidden = YES;
    }
    
}

- (void)dataZuozhe
{
    NSString *str = [NSString stringWithFormat:@"https://rong.36kr.com/api/mobi/news/%@/author-region", self.str];
    [LMTAFNetwork getUrl:str body:nil response:LMTJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        
        NSString *summary = resposeObject[@"data"][@"brief"];
        _summary.text = summary;
        
        NSArray *arr = resposeObject[@"data"][@"latestArticle"];
        self.dataArr = [NewsDetailModel modelHanderWithArray:arr];
        
        [_tableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (void)dataHander
{
    NSString *str = [NSString stringWithFormat:@"https://rong.36kr.com/api/mobi/news/%@", self.str];
    
    [LMTAFNetwork getUrl:str body:nil response:LMTJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        
        NSLog(@"%@",self.str);
        
        NSDictionary *dic = resposeObject[@"data"];
        
        NSString *str = dic[@"content"];
        
        // 标题
        NSString *titleStr = dic[@"title"];
        _titleLabel.text = titleStr;
        
        // 作者头
        NSString *tou = dic[@"user"][@"avatar"];
        [_tou sd_setImageWithURL:[NSURL URLWithString:tou]];
        
        NSString *name = dic[@"user"][@"name"];
        _zuo.text = name;
        
        
        
        
        NSString *stHtml = [NSString stringWithFormat:@"<head><style>img{max-width:360px !important;}</style></head>%@", str];
        
        
        [_web loadHTMLString:stHtml baseURL:nil];
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    NewsDetailModel *m = _dataArr[indexPath.row];
    [cell.tu sd_setImageWithURL:[NSURL URLWithString:m.featureImg]];
    cell.title.text = m.summary;
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
